<?php
class Request extends Yaf_Request_Abstract{
    protected static $type = [
        0 => INPUT_GET,
        1 => INPUT_POST,
        2 => INPUT_COOKIE,
        3 => INPUT_SERVER,
        4 => INPUT_ENV,
    ];

    public static $filter = [
        0 => FILTER_SANITIZE_ENCODED,
        1 => FILTER_SANITIZE_MAGIC_QUOTES,
        2 => FILTER_SANITIZE_NUMBER_FLOAT,
        3 => FILTER_SANITIZE_NUMBER_INT,
        4 => FILTER_SANITIZE_SPECIAL_CHARS,
        5 => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
        6 => FILTER_SANITIZE_STRING,
        7 => FILTER_SANITIZE_URL,
        8 => FILTER_UNSAFE_RAW,
    ];
    protected static function quote($value = []){
        $filter = [
            0 => FILTER_VALIDATE_INT,
            1 => FILTER_VALIDATE_FLOAT,
            2 => FILTER_VALIDATE_STRING,            
            3 => FILTER_VALIDATE_BOOLEAN,
            4 => FILTER_VALIDATE_REGEXP,
            5 => FILTER_VALIDATE_IP,
            6 => FILTER_VALIDATE_EMAIL,
            7 => FILTER_VALIDATE_MAC,
            8 => FILTER_VALIDATE_URL,
        ];
        $flags = [
            0 => FILTER_REQUIRE_ARRAY,
            1 => FILTER_FORCE_ARRAY
        ];

        $result = [];
        // foreach($value as $key => $v){
        //     switch($key){
        //     case 'filter' :
        //         $result['filter'] = $filter[$v];
        //         break;
        //     case 'flags' :
        //         $result['flags'] = $flags[$v];
        //         break;
        //     };
        // }

        $result['filter'] = $filter[$value['filter']];
        $result['flags'] = $flags[$value['flags']];
        if(array_key_exists('options', $value)){
            $result['options'] = $value['options'];
        }
        return $result;

    }
    public static function filter($type_index,$name,$filter_index, $mix=null){
        if(is_array($mix) && !empty($mix)){
            $mix_quote = self::quote($mix);
            $result = filter_input(self::$type[$type_index],$name,self::$filter[$filter_index],$mix_quote);
            return $result;
        }else{
            $result = filter_input(self::$type[$type_index],$name,self::$filter[$filter_index]);
            if(empty($result) || $result == null){
                return $mix;
            }else{
                return $result;
            }
        }

    }
    public static function asget($filter_index,$name, $mix=null){
        if(is_array($mix) && !empty($mix)){
            $mix_quote = self::quote($mix);
            $result = filter_input(INPUT_GET,$name,self::$filter[$filter_index],$mix_quote);
            return $result;
        }else{
            $result = filter_input(INPUT_GET,$name,self::$filter[$filter_index]);
            if(empty($result) || $result == null){
                return $mix;
            }else{
                return $result;
            }
        }

    }
    public static function aspost($filter_index,$name, $mix=null){
        if(is_array($mix) && !empty($mix)){
            $mix_quote = self::quote($mix);
            $result = filter_input(INPUT_POST,$name,self::$filter[$filter_index],$mix_quote);
            return $result;
        }else{
            $result = filter_input(INPUT_POST,$name,self::$filter[$filter_index]);
            if(empty($result) || $result == null){
                return $mix;
            }else{
                return $result;
            }
        }

    }    
    public static function batch($type_index,$args = []){
        $args_temp = [];
        foreach($args as $key=>$a){
            if(is_array($a)){
                $args_temp[$key] = self::quote($a);
            }else{
                $args_temp[$key] = $a;
            }
        }
        $result = filter_input_array(self::$type[$type_index],$args_temp);
        return $result;
    }
    public static function val($arg,$filter_index,$mix=[],$default = null){
        $filter = [
            0 => FILTER_VALIDATE_INT,
            1 => FILTER_VALIDATE_FLOAT,
            2 => FILTER_VALIDATE_BOOLEAN,
            3 => FILTER_VALIDATE_REGEXP,
            4 => FILTER_VALIDATE_IP,
            5 => FILTER_VALIDATE_EMAIL,
            6 => FILTER_VALIDATE_MAC,
            7 => FILTER_VALIDATE_URL,
        ];        
        $flags = [
            0 =>FILTER_FLAG_STRIP_LOW,
            1 =>FILTER_FLAG_STRIP_HIGH, 
            2 =>FILTER_FLAG_ALLOW_FRACTION,
            3 =>FILTER_FLAG_ALLOW_THOUSAND,
            4 =>FILTER_FLAG_ALLOW_SCIENTIFIC,
            5 =>FILTER_FLAG_NO_ENCODE_QUOTES,
            6 =>FILTER_FLAG_ENCODE_LOW,
            7 =>FILTER_FLAG_ENCODE_HIGH,
            8 =>FILTER_FLAG_ENCODE_AMP,
            9 =>FILTER_NULL_ON_FAILURE,
            10 =>FILTER_FLAG_ALLOW_OCTAL,
            11 =>FILTER_FLAG_ALLOW_HEX,
            12 =>FILTER_FLAG_IPV4,
            13 =>FILTER_FLAG_IPV6,
            14 =>FILTER_FLAG_NO_PRIV_RANGE,
            15 =>FILTER_FLAG_NO_RES_RANGE,
            16 =>FILTER_FLAG_PATH_REQUIRED,
            17 =>FILTER_FLAG_QUERY_REQUIRED,
            18 =>FILTER_REQUIRE_SCALAR,
            19 =>FILTER_REQUIRE_ARRAY,
            20 =>FILTER_FORCE_ARRAY,
        ];
        $result = [];
        if(is_array($arg)){
            if(empty($mix)){
                foreach($arg as $key=>$value){
                    $result[$key] = filter_var($value,$filter[$filter_index]);
                }
            }else{
                $mix_temp['flags'] = $flags[$mix['flags']];
                $mix_temp['options'] = $mix['options'];
                foreach($arg as $value){
                    $result[$key] = filter_var($value,$filter[$filter_index], $mix_temp);
                }
            }
            return $result;
        }else{
            if(empty($mix)){
                $result = filter_var($arg,$filter[$filter_index]);
            }else{
                $mix_temp['options'] = $mix['options'];
                if(isset($mix['flags'])){
                    $mix_temp['flags'] = $flags[$mix['flags']];
                }
                $result[0] = filter_var($arg,$filter[$filter_index], $mix_temp);
            }
            return $result;
        }
    }
    public static function ip() {
        if( !empty($_SERVER['REMOTE_ADDR'])){
            $client_ip = $_SERVER['REMOTE_ADDR'];
        }else{
            $client_ip = '0.0.0.0';
        }
        return $client_ip;
    }
}